package com.tma.internship.activities;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.tma.internship.ClassesInfo.FileInfo;
import com.tma.internship.ClassesInfo.ListFileAdapter;
import com.tma.internship.connectserver.ContactServer;
import com.tma.internship.connectserver.ReceiveTextFromServer;
import com.tma.internship.connectserver.ServerInfo;
import com.tma.internship.connectserver.ServiceCallback;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class ListFileActivity extends Activity implements OnItemClickListener{
	private List<FileInfo> listFile;
	private ListView lView;
	private ListFileAdapter fAdapter;
	private FileInfo fileSelected;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		listFile = new ArrayList<FileInfo>();
		setContentView(R.layout.listfile);
		List<NameValuePair> lsPair = new ArrayList<NameValuePair>();
		ContactServer contact = new ContactServer(lsPair, this);
		final HashMap<String, FileInfo> hashMapFile = (HashMap<String, FileInfo>) getIntent().getSerializableExtra("listFileInfo");
		contact.receiveText(new ServiceCallback<String>() {
		final Handler handle = new Handler();
			@Override
			public void onSuccess(String result) {
				// TODO Auto-generated method stub
				try {
					Log.e("result", result);
					JSONArray jArray = new JSONArray(result);
					int length = jArray.length();
					for (int i = 0 ; i < length ; i++){
						JSONObject jObject = jArray.getJSONObject(i);
						String name = jObject.getString("name");
						int size = jObject.getInt("size");
						int page = jObject.getInt("page");
						FileInfo fInfo = null;
						if (hashMapFile != null)
							fInfo = hashMapFile.get(name);
						if (fInfo != null) listFile.add(fInfo);
						else{
							fInfo = new FileInfo(name, size, page,false);
							if (listFile.size() > 0) listFile.add(0, fInfo);
							else listFile.add(fInfo);
						}
						//fInfo.setImg(img);
					}
					handle.post(new Runnable() {						
						@Override
						public void run() {
							// TODO Auto-generated method stub
							fAdapter = new ListFileAdapter(getBaseContext(), R.layout.fileelement, listFile);
							lView = (ListView) findViewById(R.id.listview);
							lView.setAdapter(fAdapter);
							lView.setOnItemClickListener(ListFileActivity.this);
						}
					});					
				} catch (JSONException e){
					Log.e("error from response", result);
				}
			}
			
			@Override
			public void onFailure(Exception exception) {
				// TODO Auto-generated method stub
				exception.printStackTrace();
			}
		}, ServerInfo.listFile);
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		try {
			this.openFileInput(fileSelected.getName()+"_0.png");
			if (!fileSelected.isStored()){
				// move file selected to down
				fAdapter.remove(fileSelected);
				fAdapter.add(fileSelected);
			}
			fileSelected.setStored(true);
			fAdapter.notifyDataSetChanged();
			
		} catch (Exception e){
			return;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) {
		// TODO Auto-generated method stub
		fileSelected = fAdapter.getItem(position);
		if (!fileSelected.isStored()){
			Intent intent = new Intent(this,DownloadFileActivity.class);
			intent.putExtra("name", fileSelected.getName());
			intent.putExtra("page", fileSelected.getNumOfPage());
			startActivity(intent);
		} else {
			Intent intent = new Intent(this,ControlFileActivity.class);
			intent.putExtra("file", fileSelected);
			startActivity(intent);
		}
	}
	
}
